package ru.papergames.battleserver.network.clientpackets.flashuser;

import ru.papergames.Config;
import ru.papergames.battleserver.model.PaperGameFlashUser;
import ru.papergames.battleserver.model.battle.AbstractBattle;
import ru.papergames.battleserver.model.battle.BattlePlayer;
import ru.papergames.battleserver.network.serverpackets.flashuser.SystemMessage;
import ru.papergames.battleserver.network.serverpackets.flashuser.SystemMessageId;

/**
 * @author Alexander Troshanin
 */
public abstract class AbstractBattleAction extends PaperGameClientPacket {
    @Override
    protected final void runImpl() {
        PaperGameFlashUser usr = getClient().getUser();
        usr.setLastPingTime(System.currentTimeMillis());
        AbstractBattle battle = usr.getBattle();
        BattlePlayer battlePlayer = usr.getBattleMember();
        if (battle == null || !battle.isBattleRunning()) {
            logger.debug("Cant process action, not in battle, player: " + usr.getPlayer().getUsername());
            return;
        }
        if (!battle.isPlayerMovingNow(battlePlayer)) {
            getClient().sendPacket(new SystemMessage(SystemMessageId.NOT_YOUR_TURN));
            return;
        }

        runImplInternal(battle, battlePlayer);
    }

    protected abstract void runImplInternal(AbstractBattle battle, BattlePlayer battlePlayer);

}
